<?php							
	class CUsers extends database
	{
		var $table = 'cdb_users';
			
			
		public function CUserIsUser($username, $password)
		{
			$sql="select * from {$this->table}";
			//$sql="select * from {$this->table} where username="'.$username.' and password="'.md5($password).'";			
			$this->setQuery($sql);
			$this->query();
			$arrUser= $this->loadAllRow();
			
			$isSuccess = mysql_affected_rows();
			
			if($isSuccess >0)
			{						
				/*if(is_array($arrUser))
				{	*/foreach($arrUser as $key=> $value)
					{
						if($username === $value['username']  && md5($password) === $value['password'])
							return $value;
					}
				/*}*/
			}
			return -1;
		}
			
		public function CUsersList ()
		{
			$sql = 'select * from {$this->table} '	;
			$this->setQuery($sql);
			$this->query();
			$arrTemp =  $this->loadAllRow();
			

			return $arrTemp; 
		}
		
		public function CUsersDetail ($id)
		{
			$sql="select * from $this->table where id='{$id}'";
			$this->setQuery($sql);
			$this->query();
			$arr=$this->loadAllRow();
			
			$isSuccess = mysql_affected_rows();
			
			if($isSuccess > 0)
				return $arr[0];
			else
				return -1;
		}
		
		
		
		public function CUsersAdd ($id,$username,$password,$lastname,$firstname,$gender,$birthday,$cellphone,$address,$status,$priority)
		{
			$sql = "INSERT INTO {$this->table} (`id`,`username`,`password`,`lastname`,`firstname`,`gender`,`birthday`,`cellphone`,`address`,`status`,`priority`)
VALUES ('{$id}','{$username}','{$password}','{$lastname}','{$firstname}','{$gender}','{$birthday}','{$cellphone}','{$address}','{$status}','{$priority}');";
			
			$this->setQuery($sql);
			$this->query();	
			
			$isSuccess = mysql_affected_rows();
			
			if($isSuccess > 0)
				return $isSuccess;
			else
				return -1;
			
		}
		
		public function CUsersUpdate ($id,$lastname,$firstname,$gender,$birthday,$cellphone,$address)
		{
			$sql = "update $this->table set lastname ='{$lastname}',firstname ='{$firstname}',gender ='{$gender}',birthday ='{$birthday}',cellphone ='{$cellphone}',address ='{$address}' where id = '{$id}' ";
		
			$this->setQuery($sql);
			$this->query();
			
			$isSuccess = mysql_affected_rows();
			
			if($isSuccess > 0)
				return $isSuccess;
			else
				return -1;
		}
		
		public function CUsersDelete($id)
		{
			$sql="Delete from  {$this->table} where id='{$id}'";
			$this->setQuery($sql);
			$this->query();
			
			$isSuccess = mysql_affected_rows();
			
			if($isSuccess > 0)
				return $isSuccess;
			else
				return -1;
		}
		
		public function CUsersCheckPass ($id, $username,$hashPass)
		{
			$sql = "select username,password from {$this->table} where id='{$id}' and username='{$username}'";
						
			$this->setQuery($sql);
			$this->query();
			
			echo "<br>". $isSuccess = mysql_affected_rows();
			
			if($isSuccess > 0)
			{
				$arr=$this->loadAllRow();
				
				if(is_array($arr))
				{
					if($hashPass == $arr[0]['password'])
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			else
				return false;
			
		}
		
		public function CUsersUpdatePass ($id, $hashNewPass)
		{
			$sql = "update $this->table set password ='{$hashNewPass}' where id = '{$id}' ";
		
			$this->setQuery($sql);
			$this->query();
			
			$isSuccess = mysql_affected_rows();
			
			if($isSuccess > 0)
				return $isSuccess;
			else
				return -1;
		}
		
	}
	
	
	
	?>